//
//  KBMoreListenViewController.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//
// 精品听单 更多页面
#import "KBMoreListenViewController.h"

@interface KBMoreListenViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UITableView *myTableView;
@property (nonatomic, assign)NSInteger top; // 用来刷新

@end

@implementation KBMoreListenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 取消渲染 让左右两边的按钮变色
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.969 green:0.427 blue:0.071 alpha:1.000];
    self.title = _myTitle;
    _top = 10;
    _dataArray = [NSMutableArray array];
    _myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStyleGrouped];
    // 代理
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    
    // 注册
    [_myTableView registerClass:[KBMoreListenCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_myTableView];
    [self getData];
}

- (void)getData
{
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_list?device=android&page=1&per_page=%ld", _top];
    
    [AFNetworkTool getUrl:url body:self result:HXJSON headerFile:nil success:^(id result) {
        NSLog(@"result = %@", result);
        NSMutableArray *list = [result objectForKey:@"list"];
        
        for (NSDictionary *dic in list)
        {
            KBMoreListenModel *model = [[KBMoreListenModel alloc] initWithDictionary:dic];
            [_dataArray addObject:model];
        }
        
        [self.myTableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"数据请求失败");
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    KBMoreListenCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    KBMoreListenModel *model = [_dataArray objectAtIndex:indexPath.section];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.model = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 86;
}
// 设置区头View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    KBMoreListenModel *model = [_dataArray objectAtIndex:section];
    KBMoreListenHeaderView *myView = [[KBMoreListenHeaderView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH, SCREEN_WIDTH*0.08)];
    NSInteger year = model.releasedAt / 1000;
    NSLog(@"%@", [KBYearMoonDayTools timestampExchange:year])
    myView.releasedAt.text = [KBYearMoonDayTools timestampExchange:year];
    
    return myView;
}
// 区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SCREEN_WIDTH*0.08;
}
// 点击cell跳转页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KBMoreListenModel *model = [_dataArray objectAtIndex:indexPath.section];
    KBMusicListViewController *musicListVC = [[KBMusicListViewController alloc] init];
    
    musicListVC.specialId = [NSString stringWithFormat:@"%ld", model.specialId];
    
    [self.navigationController pushViewController:musicListVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
